#include "game.h"

TestGame::TestGame() 
{

}

TestGame::~TestGame() 
{ 

}
bool TestGame::OnInit() 
{
	// Clear the screen
	_clearScreen = true;

	// Clear to black
	g_renderer.SetClearColor(0xFF000000);

	// Initialize the intro
	if (!m_intro.Initialize())
		return false;

	// Initialize the menu
	if (!m_menu.Initialize())
		return false;

	// Initialize the black screen
	if (!m_loading.LoadIni("data/graphics/loading.spr")) 
		return false;

	// Initialize the inGame stage
	m_iStage = STAGE_INTRO;

	// Lock FPS
	///////////
	_fpsMeter.SetLockFPS(30);


	return true;
}

void TestGame::OnFrame() 
{

	if (KeyDown(DIK_F11)) 
	{
		g_window.SaveScreenshot("screenshot.bmp", 800,600,32);
	}

	// Stage verify
	switch(m_iStage) 
	{
	
		case STAGE_INTRO:
			
			m_intro.Update((float)_fpsMeter.GetDT());

			if (m_intro.GetStage() == INTRO_STAGE_NONE) 
			{
				m_iStage = STAGE_MENU;
			}

			break;

		case STAGE_MENU:
			
			m_menu.Update((float)_fpsMeter.GetDT());

			if (m_menu.GetStage() == MENU_STAGE_EXIT) 
			{
				PostQuitMessage(0);

			} 

			else if (m_menu.GetStage() == MENU_STAGE_PLAY) 
			{
				m_iStage = STAGE_LOADING;
			}

			break;

		case STAGE_LOADING:
			
			m_loading.Update((float)_fpsMeter.GetDT());

			if (m_inGame.Initialize(START_LEVEL))
			{
				m_iStage = STAGE_INGAME;
			}

			else
			{
				PostQuitMessage(0);
			}

			break;

		case STAGE_INGAME:

			m_inGame.Update((float)_fpsMeter.GetDT());

			if (m_inGame.GetStage() == GAME_STAGE_EXIT) 
			{
				if (!m_inGame.Shutdown()) 
				{
					PostQuitMessage(0);
				}
				
				m_iStage = STAGE_MENU;

				m_menu.SetStage(MENU_STAGE_NONE);

			} 
			break;
	}
}

void TestGame::OnDraw() 
{
	switch(m_iStage)
	{
		case STAGE_INTRO:
			m_intro.Draw();
			break;
		case STAGE_MENU:
			m_menu.Draw();
			break;
		case STAGE_LOADING:
			m_loading.Draw();
			break;
		case STAGE_INGAME:
			m_inGame.Draw();
			break;
	}

	wstringstream ss;

	ss << "FPS: " << _fpsMeter.GetFps();
	g_renderer.SetFontColor(0xFFFF0000);
	g_renderer.DrawString(ss.str(),10,10,780,580);
}

bool TestGame::OnShutdown() {
	return true;
}